package service

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/repository"
	"boy-go/modules/system/vo"
	"boy-go/pkg/request"
	"github.com/jinzhu/copier"
)

func NewSysOperLogService() SysOperLogService {
	return &sysOperLogService{
		SysOperLogRepo: repository.NewSysOperLogRepository(),
	}
}

type sysOperLogService struct {
	SysOperLogRepo *repository.SysOperLogRepository
}

// GetById 根据id获取配置信息
func (s *sysOperLogService) GetById(operId int64) (*vo.SysOperLogModel, error) {
	m, err := s.SysOperLogRepo.GetById(operId)
	if err != nil {
		return nil, err
	}
	rsp := new(vo.SysOperLogModel)
	if err := copier.Copy(rsp, m); err != nil {
		return nil, err
	}
	return rsp, nil
}

// List 获取操作日志记录列表
func (s *sysOperLogService) List(req *vo.QuerySysOperLogReq) (*vo.ListSysOperLogRsp, error) {
	var pageReq = &request.PageRequest{
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	}
	list, total, err := s.SysOperLogRepo.List(req, pageReq)
	if err != nil {
		return nil, err
	}
	itemList := make([]*vo.SysOperLogModel, 0)
	for _, v := range list {
		item := new(vo.SysOperLogModel)
		if err := copier.Copy(item, &v); err != nil {
			continue
		}
		itemList = append(itemList, item)
	}
	return &vo.ListSysOperLogRsp{
		List:  itemList,
		Total: total,
	}, nil
}

// Add 添加操作日志记录
func (s *sysOperLogService) Add(req *vo.AddSysOperLogReq) (*vo.AddSysOperLogRsp, error) {
	m := new(model.SysOperLog)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysOperLogRepo.Save(m); err != nil {
		return nil, err
	}
	return &vo.AddSysOperLogRsp{
		OperId: m.OperId,
	}, nil
}

// Edit 修改操作日志记录
func (s *sysOperLogService) Edit(req *vo.EditSysOperLogReq) (*vo.EditSysOperLogRsp, error) {
	m := new(model.SysOperLog)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysOperLogRepo.Update(m); err != nil {
		return nil, err
	}
	return &vo.EditSysOperLogRsp{
		OperId: m.OperId,
	}, nil
}

// Del 删除操作日志记录
func (s *sysOperLogService) Del(operId int64) error {
	return s.SysOperLogRepo.Delete(operId)
}

func (s *sysOperLogService) BatchDel(ids []string) error {
	return s.SysOperLogRepo.BatchDelete(ids)
}

func (s *sysOperLogService) Clean() error {
	return s.SysOperLogRepo.Clean()
}
